/*
   Copyright (c) 2011, 2012, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *  2012-05-05 Marco Riccardi - added classes Implies, Ex, Or, Iff
 *  2015-01-11 Marco Riccardi - added class FlexFrm
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public abstract class Formula implements INode {

    private enum MySubclass { Is, For, And, Not, Pred, PrivPred, Verum, ErrorFrm, Implies, Ex, Or, Iff, FlexFrm}

    public static boolean isFormula(String className){
        boolean found = false;
        for (MySubclass mySubclass :MySubclass.values()){
            if (mySubclass.name().equals(className)){
                found = true;
                break;
            }
        }
        return found;
    }

    public static Formula newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        XMLElement xmlElement = xmlElementList.peek();
        if (!isFormula(xmlElement.getName())){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of Formula", xmlElementList);
        }
        MySubclass mySubclass = MySubclass.valueOf(xmlElement.getName());
        switch (mySubclass) {
        case Is: return Is.newInstance(parentNode, xmlElementList);
        case For: return For.newInstance(parentNode, xmlElementList);
        case And: return And.newInstance(parentNode, xmlElementList);
        case Not: return Not.newInstance(parentNode, xmlElementList);
        case Pred: return Pred.newInstance(parentNode, xmlElementList);
        case PrivPred: return PrivPred.newInstance(parentNode, xmlElementList);
        case Verum: return Verum.newInstance(parentNode, xmlElementList);
        case ErrorFrm: return ErrorFrm.newInstance(parentNode, xmlElementList);
        case Implies: return Implies.newInstance(parentNode, xmlElementList);
        case Ex: return Ex.newInstance(parentNode, xmlElementList);
        case Or: return Or.newInstance(parentNode, xmlElementList);
        case Iff: return Iff.newInstance(parentNode, xmlElementList);
        case FlexFrm: return FlexFrm.newInstance(parentNode, xmlElementList);
        default: throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of Formula", xmlElementList);
        }
    }

        public LinkedList<XMLElement> getXMLElementList() {
            if (this instanceof Is) {
                return ((Is) this).getXMLElementList();
            } else if (this instanceof For) {
                return ((For) this).getXMLElementList();
            } else if (this instanceof And) {
                return ((And) this).getXMLElementList();
            } else if (this instanceof Not) {
                return ((Not) this).getXMLElementList();
            } else if (this instanceof Pred) {
                return ((Pred) this).getXMLElementList();
            } else if (this instanceof PrivPred) {
                return ((PrivPred) this).getXMLElementList();
            } else if (this instanceof Verum) {
                return ((Verum) this).getXMLElementList();
            } else if (this instanceof Implies) {
                return ((Implies) this).getXMLElementList();
            } else if (this instanceof Ex) {
                return ((Ex) this).getXMLElementList();
            } else if (this instanceof Or) {
                return ((Or) this).getXMLElementList();
            } else if (this instanceof Iff) {
                return ((Iff) this).getXMLElementList();
            } else if (this instanceof FlexFrm) {
                return ((FlexFrm) this).getXMLElementList();
            } else 
            return new LinkedList<XMLElement>();
        }
}
